﻿using System;
using Microsoft.Practices.ObjectBuilder;
using Microsoft.Practices.CompositeWeb.Web.UI;
using System.Collections.Generic;
using System.Web.Script.Serialization;
using DataAccess.BusinessEntities;
using Microsoft.Practices.Web.UI.WebControls;

namespace Locator.Stores.Views
{
    public partial class StoreSearch : UserControl, IStoreSearchView
    {
        private StoreSearchPresenter presenter;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                this.presenter.OnViewInitialized();
            }

            this.presenter.OnViewLoaded();
        }

        [CreateNew]
        public StoreSearchPresenter Presenter
        {
            get { return this.presenter; }
            set
            {
                if (value == null)
                    throw new ArgumentNullException("value");

                this.presenter = value;
                this.presenter.View = this;
            }
        }

        protected void ButtonSearchByCity_Click(object sender, EventArgs e)
        {
            this.presenter.OnSearchByCityClicked();
        }

        public void SetSearchResults(List<Store> stores)
        {
            this.StoresDataSource.DataSource = stores;
        }

        public string CityToSearch
        {
            get { return this.TextBoxCity.Text; }
        }

        public string PostalCodeToSearch
        {
            get { return this.TextBoxPostalCode.Text; }
        }

        public int PostalCodeSearchRadius
        {
            get { return Convert.ToInt32(this.DropDownListPostalCodeRadius.SelectedValue); }
        }

        protected void StoresDataSource_Updated(object sender, ObjectContainerDataSourceStatusEventArgs e)
        {
            this.presenter.OnStoreAddedForUser((Store)e.Instance);
        }

        public List<string> Cities { get; set; }

        public string CitiesAsJson
        {
            get { return new JavaScriptSerializer().Serialize(Cities); }
        }

        /// <summary>
        /// Sets the search radii.
        /// </summary>
        /// <param name="searchRadii">The search radii.</param>
        public void SetSearchRadii(List<int> searchRadii)
        {
            this.SearchRadiiDataSource.DataSource = searchRadii;
        }

        public int PersonalLocationId
        {
            get { return Int32.Parse(this.DropDownListPersonalLocations.SelectedValue); }
        }

        public int PersonalLocationSearchRadius
        {
            get { return Convert.ToInt32(this.DropDownListSearchRadiusPersonalLocation.SelectedValue); }
        }

        /// <summary>
        /// Sets the personal locations.
        /// </summary>
        /// <param name="personalLocations">The personal locations.</param>
        public void SetPersonalLocations(List<PersonalLocation> personalLocations)
        {
            this.PersonalLocationDataSource.DataSource = personalLocations;
        }

        protected void ButtonSearchByPostalCode_Click(object sender, EventArgs e)
        {
            this.presenter.OnSearchByPostalCodeClicked();
        }

        protected void ButtonSearchByPersonalLocation_Click(object sender, EventArgs e)
        {
            this.presenter.OnSearchByPersonalLocationClicked();
        }

        public bool PersonalLocationSearchEnabled
        {
            get { return this.ButtonSearchByPersonalLocation.Enabled; }
            set { this.ButtonSearchByPersonalLocation.Enabled = value; }
        }

        public override void Dispose()
        {
            if (this.presenter != null)
            {
                this.presenter.Dispose();
            }

            base.Dispose();
        }
    }
}

